package com.test.nnooolwdeaer.bean_wooden.process_wooden;

import android.text.TextUtils;
import android.util.Log;

import com.test.nnooolwdeaer.constant_wooden.Constant_wooden;
import com.test.nnooolwdeaer.constant_wooden.EnumApplyStatus_wooden;
import com.test.nnooolwdeaer.constant_wooden.EnumOrderStatus_wooden;
import com.test.nnooolwdeaer.util_wooden.StringUtil_wooden;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class ProcessInfo_wooden {

    private static class Inner_wooden {
        private static ProcessInfo_wooden instance_wooden = new ProcessInfo_wooden();
    }

    private ProcessInfo_wooden() {}

    public static ProcessInfo_wooden getInstance_wooden() {
        return Inner_wooden.instance_wooden;
    }

    public static void parse_wooden(String jsonStr) {
        Gson gson = new Gson();
        Inner_wooden.instance_wooden = gson.fromJson(jsonStr, ProcessInfo_wooden.class);
        try {
            Inner_wooden.instance_wooden.srcJSON = new JSONObject(jsonStr);
        } catch (JSONException e) {
            if (Constant_wooden.Debug) e.printStackTrace();
        }

        //判断当前订单状态显示对应的界面
        if (Inner_wooden.instance_wooden.isOrderPendingRepayment_wooden()) {//是否在待还款-未逾期
            Inner_wooden.instance_wooden.setLoanState_wooden(LoanState_wooden.Order_Pending_Repayment);
        } else if (Inner_wooden.instance_wooden.isOrderOverdueRepayment_wooden()) {//是否在待还款-已逾期
            Inner_wooden.instance_wooden.setLoanState_wooden(LoanState_wooden.Order_Overdue_Repayment);
        } else if (Inner_wooden.instance_wooden.isOrderLoaning_wooden()) {//是否在待放款
            Inner_wooden.instance_wooden.setLoanState_wooden(LoanState_wooden.Order_Loaning);
        } else if (Inner_wooden.instance_wooden.isLoanRejcted_wooden()) {//申请是否被拒绝
            Inner_wooden.instance_wooden.setLoanState_wooden(LoanState_wooden.Loan_Rejected);
        } else if (Inner_wooden.instance_wooden.isLoanReviewing_wooden()) {//是否有审核中的贷款
            Inner_wooden.instance_wooden.setLoanState_wooden(LoanState_wooden.Loan_Reviewing);
        } else {
            Inner_wooden.instance_wooden.setLoanState_wooden(LoanState_wooden.No_Order);
        }
    }

    private String product;
    private ArrayList<String> allStepTitles;
    private ArrayList<String> allSteps;
    private ArrayList<String> steps;
    private LoanState_wooden loanState;
    private String idCard;
    private String idCardImg;
    private String birthDate;
    private String applyStatus;//申请订单状态
    private String orderStatus;//还款订单状态
    private String deniedBetweenDay;//被拒重新申请天数
    private String userType;
    private JSONObject srcJSON;
    //签约
    private String signFlag;
    private String signOrderId;

    private String recipientName;

    //越南额外流程
    private ArrayList<String> extStep;
    private String ocr_info;

    public String getProduct_wooden() {
        return StringUtil_wooden.getSafeString_wooden(product);
    }

    public int getStepsSize_wooden(){
        if (steps==null) return 0;
        return steps.size();
    }

    public String getIdCard_wooden() {
        return StringUtil_wooden.getSafeString_wooden(idCard);
    }

    public String getIdCardImg_wooden() {
        return StringUtil_wooden.getSafeString_wooden(idCardImg);
    }

    public String getStepName_wooden(int index){
        if (steps==null||index<0||index>=steps.size()) return "";
        return steps.get(index);
    }

    public ProcessStep_wooden getStep_wooden(int index){
        if (steps==null||index<0||index>=steps.size()||srcJSON==null) return null;
        String stepStr = srcJSON.optString(steps.get(index));

        return new Gson().fromJson(stepStr, ProcessStep_wooden.class);
    }

    public ArrayList<String> getSteps_wooden() {
        return steps;
    }

    public void setLoanState_wooden(LoanState_wooden loanState) {
        this.loanState = loanState;
    }

    public String getOrderStatus_wooden() {
        return StringUtil_wooden.getSafeString_wooden(orderStatus);
    }

    public String getApplyStatus_wooden() {
        return StringUtil_wooden.getSafeString_wooden(applyStatus);
    }

    public String getDeniedBetweenDay_wooden() {
        if (TextUtils.isEmpty(deniedBetweenDay)) return "30";
        return StringUtil_wooden.getSafeString_wooden(deniedBetweenDay);
    }

    public ArrayList<String> getAllSteps_wooden() {
        if (allSteps==null) return new ArrayList<>();
        return allSteps;
    }

    public String getRecipientName_wooden() {
        return StringUtil_wooden.getSafeString_wooden(recipientName);
    }

    public boolean isNewUser_wooden(){
        return !StringUtil_wooden.getSafeString_wooden(userType).equalsIgnoreCase("OLD");
    }

    public String getSignOrderId() {
        return StringUtil_wooden.getSafeString_wooden(signOrderId);
    }

    public boolean isNeedSign(){
        return StringUtil_wooden.getSafeString_wooden(signFlag).equalsIgnoreCase("YES")&&
                getSignOrderId().length()>0;
    }

    public ArrayList<String> getAllStepTitles_wooden() {
        return allStepTitles;
    }

    public String getStepTitle_wooden(){
        if (allStepTitles==null||allStepTitles.size()<allSteps.size()) return "";
        String stepName = getStepName_wooden(0);
        int i = allSteps.indexOf(stepName);
        if (i<0||i>=allStepTitles.size()) return "";
        return allStepTitles.get(i);
    }

    public ArrayList<String> getExtStep() {
        if (extStep==null) return new ArrayList<>();
        return extStep;
    }

    public boolean ocrInfoIsSDK() {
        return !TextUtils.isEmpty(ocr_info) && ocr_info.equalsIgnoreCase("SDK");
    }

    /**
     * 待放款
     */
    public boolean isOrderLoaning_wooden() {
        return (getOrderStatus_wooden().equalsIgnoreCase(EnumOrderStatus_wooden.CREATED_wooden) ||
                getOrderStatus_wooden().equalsIgnoreCase(EnumOrderStatus_wooden.LENDING_wooden));
    }

    /**
     * 待还款-未逾期
     */
    public boolean isOrderPendingRepayment_wooden() {
        return getOrderStatus_wooden().equalsIgnoreCase(EnumOrderStatus_wooden.LOANED_wooden);
    }

    /**
     * 待还款-已逾期
     */
    public boolean isOrderOverdueRepayment_wooden() {
        return (getOrderStatus_wooden().equalsIgnoreCase(EnumOrderStatus_wooden.OVERDUE_wooden) ||
                getOrderStatus_wooden().equalsIgnoreCase(EnumOrderStatus_wooden.BAD_DEBT_wooden));
    }

    /**
     * 申请拒绝
     */
    public boolean isLoanRejcted_wooden() {
        return (getOrderStatus_wooden().equalsIgnoreCase(EnumOrderStatus_wooden.DENIED_wooden) ||
                getApplyStatus_wooden().equalsIgnoreCase(EnumApplyStatus_wooden.SYS_DENIED_wooden) ||
                getApplyStatus_wooden().equalsIgnoreCase(EnumApplyStatus_wooden.MANU_DENIED_wooden) ||
                getApplyStatus_wooden().equalsIgnoreCase(EnumApplyStatus_wooden.DS_DENIED_wooden));
    }

    /**
     * 申请审核中
     */
    public boolean isLoanReviewing_wooden() {
        return (getApplyStatus_wooden().equalsIgnoreCase(EnumApplyStatus_wooden.CREATED_wooden) ||
                getApplyStatus_wooden().equalsIgnoreCase(EnumApplyStatus_wooden.SYS_CHECK_wooden) ||
                getApplyStatus_wooden().equalsIgnoreCase(EnumApplyStatus_wooden.SYS_APROVAL_wooden) ||
                getApplyStatus_wooden().equalsIgnoreCase(EnumApplyStatus_wooden.MANU_APROVAL_wooden) ||
                getApplyStatus_wooden().equalsIgnoreCase(EnumApplyStatus_wooden.DS_APROVAL_wooden));
    }

    public enum LoanState_wooden {
        ///////////////////
        No_Order,//没有订单
        Loan_Rejected,//申请拒绝
        Loan_Reviewing,//申请审核中
        Order_Loaning,//待放款
        Order_Pending_Repayment,//待还款，未逾期
        Order_Overdue_Repayment,//待还款,已逾期
    }
}
